package com.rljk.controller.backend;

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.service.CaidanManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author:   YASUO
 * Date:     2018/6/25 10:13
 * Description:
 */
@RestController
@RequestMapping("/caidan")
@Slf4j
public class CaidanController extends BaseController{


    @Resource(name = "caidanService")
    private CaidanManager caidanService;


    /**
     * 后台添加菜单
     * author:YASUO
     * date:2018/6/25
     * * @param null
     */
    @RequestMapping(value="/add")
    public Object addCaidan(){
        log.info("后台添加菜单");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("ADDCAIDAN", pd.getString("FKEY"))){	//检验请求key值是否合法
                if(AppUtil.checkParam("addCaidan", pd)){	//检查参数
                    log.info("请求参数："+pd.toString());
                    pd.put("CAIDAN_ID",this.getSnowId());
                    caidanService.save(pd);
                    return Result.success(null);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * 后台删除菜单
     * author:zhang
     * date:2018/6/25
     * * @param CAIDAN_ID
     */
    @RequestMapping(value="/del")
    public Object delCaidan(){
        log.info("后台删除菜单");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("DELCAIDAN", pd.getString("FKEY"))){	//检验请求key值是否合法
                if(AppUtil.checkParam("delCaidan", pd)){	//检查参数
                    log.info("请求参数："+pd.toString());
                    caidanService.delete(pd);
                    return Result.success(null);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * 后台修改菜单
     * @author zhang
     * @date 2018/6/25 17:43
     */
    @RequestMapping(value="/edit")
    public Object editCaidan(){
        log.info("后台修改菜单");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("EDITCAIDAN", pd.getString("FKEY"))){	//检验请求key值是否合法
                if(AppUtil.checkParam("editCaidan", pd)){	//检查参数
                    log.info("请求参数："+pd.toString());
                    caidanService.edit(pd);
                    return Result.success(null);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     *  后台菜单列表（全部）
     * @author zhang
     * @date 2018/6/26 10:15
     */
    @RequestMapping(value="/listAll")
    @ResponseBody
    public Object caidanListAll(){
        log.info("后台菜单列表（全部）");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("CAIDANLISTALL", pd.getString("FKEY"))){	//检验请求key值是否合法
                if(AppUtil.checkParam("caidanListAll", pd)){	//检查参数
                    log.info("请求参数："+pd.toString());
                    String caidan_type = (String) pd.get("CAIDAN_TYPE");
                    pd.put("CAIDAN_TYPE",caidan_type);
                    List<PageData> listAll = caidanService.listAll(pd);
                    return Result.success(listAll);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * 后台通过caidanId获取数据
     * @author zhang
     * @date 2018/6/26 10:45
     */
    @RequestMapping(value="/caidanInfo")
    public Object caidanById(){
        log.info("后台通过id获取数据");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("CAIDANINFO", pd.getString("FKEY"))){	//检验请求key值是否合法
                if(AppUtil.checkParam("caidanById", pd)){	//检查参数
                    log.info("请求参数："+pd.toString());
                    PageData caidanInfo = caidanService.findById(pd);
                    return Result.success(caidanInfo);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     *
     * @author zhang
     * @date 2018/6/25 16:42
     */
    @RequestMapping(value="/test")
    public Object test(){
        log.info("test");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            return Result.success(null);
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

}
